package com.xinqi.common.third.mail;

import com.xinqi.common.third.mail.help.MailHelp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;

import javax.mail.internet.MimeMessage;

/**
 * @author linkai
 * @date 2023/2/21
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.mail", name = {"host", "username"})
@ConditionalOnClass({JavaMailSender.class, MimeMessage.class})
@AutoConfigureAfter(MailSenderAutoConfiguration.class)
public class MailConfig {
    @Value("${spring.mail.username}")
    private String defaultFrom;

    @Bean
    public MailHelp mailHelp(JavaMailSender javaMailSender) {
        return new MailHelp(javaMailSender, defaultFrom);
    }
}
